package com.zzf.concurrent.example.design.pattern.balking;

import com.zzf.concurrent.common.util.StringUtil;
import com.zzf.concurrent.example.util.TimeUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @author GaoFeng2017
 * @date 2019/9/8 19:29
 */
@Slf4j
public class ChangerThread extends Thread {

    private Data data;

    public ChangerThread(Data data) {
        this.data = data;
    }

    @Override
    public void run() {
        while (true) {
            String fileName = "H:\\API\\project_test\\concurrent-example\\balking\\" +
                    StringUtil.getRandomStr(5) + ".txt";

            String randomStr = StringUtil.getRandomStr(32);
            log.info("change content, fileName is {}, content is {}", fileName, randomStr);
            data.change(fileName, randomStr);
            TimeUtil.sleep((long) (Math.random() * 1000));
            boolean isSave = data.save();
            log.info("data is saved after change, {}", isSave);
        }
    }
}
